package com.shaoshaohuo.app.ui;

import android.animation.ArgbEvaluator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ShareKey;
import com.shaoshaohuo.app.entity.NewPickEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.NewHttpCofig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.ui.fragment.NewPicDetailsFragment;
import com.shaoshaohuo.app.ui.fragment.PickDetailsFragmentTop;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.sharesdk.ShareContent;
import com.shaoshaohuo.app.view.DefaultTransformer;
import com.shaoshaohuo.app.view.ShareDialog;
import com.shaoshaohuo.app.view.Tablayuot;
import com.shaoshaohuo.app.view.VerticalViewPager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 详情页
 * Created by Administrator on 2017/6/9.
 */

public class PickDetailsActivity extends BaseActivity implements View.OnClickListener {
    private VerticalViewPager ac_detalis_vp;
    private Handler handler = new Handler();
    private List<Fragment> fragments = new ArrayList<>();
    private ArgbEvaluator argbEvaluator;
    private RelativeLayout detalisActivity_relatopbar;
    private Tablayuot detalisActivity_tv_shopnmae;
    private ImageView detalisActivity_share;
    private String catid;
    private ImageView detalisActivity_back;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detalayout);
        detalisActivity_share = (ImageView) findViewById(R.id.detalisActivity_share);

        ViewGroup content = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
        View childAt = content.getChildAt(0);
        changeTransltestaus(this, childAt, TransltestausModel.NoePicter, Color.parseColor("#B2000000"));
        detalisActivity_back = (ImageView) findViewById(R.id.detalisActivity_back);
        Intent intent = getIntent();
        catid = intent.getStringExtra(ShareKey.CATID);
        ac_detalis_vp = (VerticalViewPager) findViewById(R.id.ac_detalis_vp);
        detalisActivity_relatopbar = (RelativeLayout) findViewById(R.id.detalisActivity_relatopbar);
        detalisActivity_tv_shopnmae = (Tablayuot) findViewById(R.id.detalisActivity_tv_shopnmae);
        detalisActivity_tv_shopnmae.setVisibility(View.VISIBLE);

        detalisActivity_tv_shopnmae.setTabseleterlistener(new Tablayuot.Tabseleterlistener() {
            @Override
            public void tabselter(View view, int index) {
                Intent intent1 = new Intent();
                intent1.setAction("tabselter");
                intent1.putExtra("postion",index);
                sendBroadcast(intent1);
            }
        });
        ArrayList<String> strings = new ArrayList<>();
        strings.add("商品详情");
        strings.add("评价");
        ViewGroup parent = (ViewGroup) detalisActivity_tv_shopnmae.getParent();
        detalisActivity_tv_shopnmae.setTranslationY(parent.getLayoutParams().height);
        detalisActivity_tv_shopnmae.settab(strings, getResources().getDisplayMetrics().widthPixels / 5, parent.getLayoutParams().height);
        argbEvaluator = new ArgbEvaluator();


        DefaultTransformer defaultTransformer = new DefaultTransformer();

        ac_detalis_vp.setPageTransformer(false, defaultTransformer);
        defaultTransformer.setTransformerListener(new DefaultTransformer.TransformerListener() {
            @Override
            public void transformapge(View view, float position) {
                if (view instanceof RelativeLayout) {
                    int evaluate = (int) argbEvaluator.evaluate(Math.abs(position), 0x00ffffff, 0xffffffff);
                    detalisActivity_relatopbar.setBackgroundColor(evaluate);
                    detalisActivity_tv_shopnmae.setTranslationY((1 - Math.abs(position)) * detalisActivity_tv_shopnmae.getHeight());
                    if (position == -1) {
                        detalisActivity_share.setVisibility(View.GONE);
                        detalisActivity_back.setImageResource(R.mipmap.fanhui_);
                    }
                    if (position == 0) {
                        detalisActivity_share.setVisibility(View.VISIBLE);
                        //@mipmap/returnb
                        detalisActivity_back.setImageResource(R.mipmap.returnb);
                    }
                    if (position < 0 && position > -1) {
                        ac_detalis_vp.setaBoolean(false);
                    } else {
                        ac_detalis_vp.setaBoolean(true);
                    }
                } else {
                    if (position > 0 && position < 1) {
                        ac_detalis_vp.setaBoolean(false);
                    } else {
                        if (ac_detalis_vp.getCurrentItem() == 1) {
                            ac_detalis_vp.setaBoolean(true);
                        }

                    }
                }

            }
        });
        //解析
        Map<String, String> headerParams = RequestParam.getHeaderParams();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("catid", catid);
        Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + NewHttpCofig.App_Details, stringStringHashMap, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final NewPickEntity detailsEntity = (NewPickEntity) o;
                NewPickEntity.DataBean data = detailsEntity.getData();
                String name = data.getName();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        PickDetailsFragmentTop detailsFragmentTop = PickDetailsFragmentTop.newInstance(detailsEntity);
                        NewPicDetailsFragment detailsFragmentBottom = NewPicDetailsFragment.newInstance(detailsEntity);
                        fragments.add(detailsFragmentTop);
                        fragments.add(detailsFragmentBottom);
                        Myadapter myadapter = new Myadapter(getSupportFragmentManager());
                        ac_detalis_vp.setAdapter(myadapter);

                    }
                });

            }
        }, NewPickEntity.class);
//        ac_detalis_vp.setPageTransformer(false, new ViewPager.PageTransformer() {
//            @Override
//            public void transformPage(View view, float position) {
//                Log.e("TAG", "id: " + view + " position:" + position);
//
//            }
//        });
        detalisActivity_share.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detalisActivity_share:
                ShareDialog shareDialog = new ShareDialog(v.getContext());
                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setTitle("妈妈爱果");
                shareParams.setTitleUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + catid);
                shareParams.setText(ShareContent.SHARE_APP_CONTENT);
                shareParams.setImageUrl(HttpConfig.WebPage.SHARE_IMAGE_PATH);
                shareParams.setUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + catid);
                shareDialog.setshareParams(shareParams);
                shareDialog.show();
                break;
        }
    }

    class Myadapter extends FragmentPagerAdapter {

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public void setvpseleter(int seleterpos){
        detalisActivity_tv_shopnmae.curtetab(seleterpos);
    }

    public void setAc_detalis_vp(int index) {
        ac_detalis_vp.setCurrentItem(index);
    }
}
